import {
    queryOrder,
    getOrderDetail
} from "../../utils/config";
import store from "../../utils/factory";
import util from "../../utils/util";

let that = null;
//获取应用实例
const app = getApp();

Component({
    pageLifetimes: {
        show() {
            if (typeof this.getTabBar === "function" && this.getTabBar()) {
                this.getTabBar().setData({
                    selected: 2
                });
            }
            this.setData({
                noData: false,
                finish: false,
                list: []
            })
            this.queryOrder();
        }
    },
    data: {
        showtab: 0, //顶部选项卡索引
        limit: 10,
        page: 1,
        noData: false,
        finish: false,
        status: "已预定",
        list: [],
        tabnav: {
            tabitem: [{
                    id: 1,
                    text: "已预定",
                    status: "已预定",
                },
                {
                    id: 2,
                    text: "待评价",
                    status: "待评价",
                },
                {
                    id: 3,
                    text: "已评价",
                    status: "已评价",
                }
            ]
        },
        productList: [],
    }, // 私有数据，可用于模板渲染
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function() {
        that = this;
    }, // 此处attached的声明会被lifetimes字段中的声明覆盖
    ready: function() {},
    methods: {
        pageScroll(e) {
            console.log(e.detail)
        },
        onShareAppMessage() {
            return {
                title: "月嫂服务",
                path: '/pages/index/index'
            }
        },
        setTab: function(e) {
            const edata = e.currentTarget.dataset;
            if (this.data.showtab == edata.tabindex) {
                return false
            }
            this.setData({
                showtab: edata.tabindex,
                list: [],
                page: 1,
                noData: false,
                finish: false,
                status: edata.status
            })
            this.queryOrder();
        },
        goDetail(e) {
            const edata = e.currentTarget.dataset;
            wx.navigateTo({
                url: `../orderDetail/orderDetail?id=${edata.id}`
            });
        },
        produceEndTime(date, during) {
            // var str = '2016-06-27'; //定义开始日期
            var startDate = new Date(Date.parse(date.replace(/-/g, '/'))); //将开始时间由字符串格式转换为日期格式
            // var day = 10; //定义天数​
            //  开始计算结束日期                   
            var value = startDate.getTime(); //将开始时间转为毫秒            
            value += during * (24 * 3600 * 1000); //将天数转换成毫秒后与开始时间相加得到结束时间的毫秒数         
            var endDate = new Date(value); //将得到的毫秒数转换为日期
            return endDate
        },
        onReachBottom() {
            if (this.data.finish) {
                return false
            }
            this.setData({
                page: this.data.page + 1
            })
            this.queryOrder();
        },
        queryOrder() {
            queryOrder({
                page: this.data.page,
                limit: 20,
                status: this.data.status
            }).then(res => {
                let req = res.data;
                if (req.length == 0) {
                    this.setData({
                        finish: true,
                        noData: true
                    })
                    return false
                }
                if (req.length > 0) {
                    req.map(item => {
                        item.star = parseInt(item.star);
                        item.endDate = util.formatTime(that.produceEndTime(item.startTime, item.during));
                        console.log(item.endDate)
                    })
                    if (req.length < 20) {
                        this.setData({
                            finish: true
                        })
                    }
                    that.setData({
                        list: [...that.data.list, ...req],
                    })
                }
            });
        },
    }
});